﻿function LaunchUpdateTemplatePage() {
    //alert('LaunchUpdateTemplatePage');
    SP.SOD.executeOrDelayUntilScriptLoaded(getRelativeUrl, 'SP.js');
}
function getRelativeUrl() {
    var webUrl = _spPageContextInfo.webServerRelativeUrl;
    if (webUrl == '/') webUrl = '';
    //alert('webUrl=' + webUrl);
    var options = {
        url: webUrl + '/_layouts/15/spfpub/modals/updatepagetemplate.aspx?ID=' + _spPageContextInfo.pageItemId + '&List=' + _spPageContextInfo.pageListId,
        title: 'Update Page Layout',
        allowMaximize: false,
        showClose: true,
        width: 850,
        height: 650
    };
    SP.UI.ModalDialog.showModalDialog(options);
}

function LaunchUpdateTemplatePageList() {
    //alert('LaunchUpdateTemplatePageList');
    SP.SOD.executeOrDelayUntilScriptLoaded(initialize, 'SP.js');
}
function getRelativeUrlForList() {
    webUrl = web.get_serverRelativeUrl();
    //alert(webUrl);
    if(webUrl == '/') webUrl = '';
    var items = SP.ListOperation.Selection.getSelectedItems(ctx);
    var selectedPages = '';
    var k;
    for (k in items)
    {
        selectedPages += '|' + items[k].id;
    }
    var options = {
        url: webUrl + '/_layouts/15/spfpub/modals/updatepagetemplate.aspx?items=' + selectedPages + '&source=' + SP.ListOperation.Selection.getSelectedList(),
        title: 'Update Page Layout',
        allowMaximize: false,
        showClose: true,
        width: 850,
        height: 650
    };
    SP.UI.ModalDialog.showModalDialog(options);
}
function getFailed()
{
    alert('Failed to retrieve the server relative URL.');
}
function initialize()
{
    var ctx = new SP.ClientContext.get_current();
    web = ctx.get_web();
    ctx.load(web);
    ctx.executeQueryAsync(Function.createDelegate(this, getRelativeUrlForList), Function.createDelegate(this, getFailed));
}
